const connectiondb = require("../sql/mysql.js")

// TODO 获取图书借阅信息
async function queryBook(req, res) {
    const sql = "select * from book_Lend"
    const result = await connectiondb(sql, [])
    res.send({ code: 200, msg: '获取信息成功', data: result })
}
// TODO 删除图书借阅信息
async function delBook(req, res) {
    const { book_id } = req.body
    const sql = "delete from book_Lend where book_id = ?"
    await connectiondb(sql, [book_id])
    res.send({ code: 200, msg: "删除成功" })
}
// TODO 添加图书借阅信息
async function addBook(req, res) {
    const { name, lend, number } = req.body
    const sql = "insert into book_Lend value(null,?,?,?)";
    await connectiondb(sql, [name, lend, number])
    res.send({ code: 200, msg: "添加成功" })
}
// TODO 根据id获取信息
async function findBook(req, res) {
    const { book_id } = req.body
    const sql = "select * from book_Lend where book_id = ?"
    const result = await connectiondb(sql, [book_id])
    res.send({ code: 200, msg: "获取成功", data: result[0] })
}
// TODO 修改操作
async function updateBook(req,res){
    const { book_id, name, book_lend, lend_number} = req.body;
    const sql = "update book_Lend set name = ?,book_lend = ?,lend_number = ? where book_id =?"
    await connectiondb(sql,[ name, book_lend, lend_number,book_id])
    res.send({code:200,msg:"修改成功"})

}
module.exports = {
    queryBook,
    delBook,
    addBook,
    findBook,
    updateBook
}